﻿using System;
using Castle.Core;
using Castle.Core.Interceptor;
using Castle.DynamicProxy;
using Castle.Facilities.FactorySupport;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using NUnit.Framework;

namespace WindsorInitConfig {
    [TestFixture]
    public class RegisterProxyWithoutTarget {
        [Test]
        public void UsingFactoryMethod_ExplicitNewGenerator() {
            var container = new WindsorContainer();
            container.AddFacility<FactorySupportFacility>();
            var generator = new ProxyGenerator();
            container.Register(Component.For<ISomething>()
                .UsingFactoryMethod(() => generator.CreateInterfaceProxyWithoutTarget<ISomething>(new DynamicImplementationInterceptor())));
            var something = container.Resolve<ISomething>();
            Assert.AreEqual(5, something.DoSomething());
        }

        [Test]
        public void tt() {
            var container = new WindsorContainer();
            container.Register(Component.For<DynamicImplementationInterceptor>());
            container.Register(Component.For<ISomething>()
                .Interceptors(InterceptorReference.ForType<DynamicImplementationInterceptor>()).First);
            var something = container.Resolve<ISomething>();
            Assert.AreEqual(5, something.DoSomething());
        }

        public class DynamicImplementationInterceptor: IInterceptor {
            public void Intercept(IInvocation invocation) {
                invocation.ReturnValue = 5;
            }
        }

        public interface ISomething {
            int DoSomething();
        }
    }
}